package com.huawei.statistics.service.impl.video;


import com.alibaba.fastjson.JSONObject;
import com.huawei.statistics.dao.video.VideoDao;
import com.huawei.statistics.jdong.URLFecter;
import com.huawei.statistics.pojo.video.TVPlay;
import com.huawei.statistics.pojo.video.Video;
import com.huawei.statistics.redis.impl.common.RedisTemplate;
import com.huawei.statistics.service.impl.common.BaseServiceImpl;
import com.huawei.statistics.service.video.TVPlayService;
import com.huawei.statistics.service.video.VideoService;
import com.huawei.statistics.utils.DateTimeUtilT;
import com.huawei.statistics.utils.PageUtil;
import com.huawei.statistics.utils.RandCodeUtil;
import com.huawei.statistics.utils.UploadImageOSS;
import com.huawei.statistics.vo.common.HttpResult;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by 18573 on 2018/12/8.
 */
@Service
public class VideoServiceImpl extends BaseServiceImpl implements VideoService, Runnable {

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    VideoDao videoDao;
    @Autowired
    TVPlayService tvPlayService;


    @Override
    public List<Video> getVdieoByRecommend() {
        Map<String,Object> map = new HashMap<>();
        map.put("recommend",true);
        List<Video> list = findEntityByPage(Video.class,map,1,12,"id");
        return list;
    }

    @Override
    public HttpResult getAiqiyiComic(Integer num) {

        String url = "https://list.iqiyi.com/www/4/-------------24-"+num+"-1-iqiyi--.html";
        List<Video> list = null;
        list = URLFecter.URLParser(url, 4);
        if (list == null || list.size() <= 0) {
            redisTemplate.set("动漫更新:" + RandCodeUtil.randNumberCodeByCustom("5", 10)+
                    DateTimeUtilT.dateTime(new Date()), "没有采集到数据");
            return new HttpResult(HttpResult.NULL, "没有采集到数据");
        }
        Boolean b = null;
        int success = 0;
        int unseccess = 0;
        StringBuffer videoStr = new StringBuffer("");
        StringBuffer videoStr1 = new StringBuffer("");
        videoStr.append(url + "\n");
        for (Video video : list) {

            b = saveVideo(video);
            if (b) {
                success++;
                videoStr1.append("动漫title====" + video.getTitle() + "====id=" + video.getId() + "\n");
            } else {
                unseccess++;
            }
        }
        videoStr.append("采集到电视剧总数:" + list.size() + "个，更新成功" + success + "个。失败:" + unseccess + "个\n");
        videoStr.append(videoStr1);
        redisTemplate.set("电视剧更新:" + RandCodeUtil.randNumberCodeByCustom("5", 10)+
                DateTimeUtilT.dateTime(new Date()), videoStr.toString());


        return new HttpResult(HttpResult.OK, videoStr);
    }

    @Override
    public HttpResult getAiqiyiTYplay(Integer num) {
        List<Video> list = null;
        //https://list.iqiyi.com/www/2/-------------4-1-1-iqiyi--.html
        String url = "https://list.iqiyi.com/www/2/-------------4-"+num+"-1-iqiyi--.html";
        list = URLFecter.URLParser(url, 2);
        if (list == null || list.size() <= 0) {
            redisTemplate.set("电视剧更新:" + RandCodeUtil.randNumberCodeByCustom("5", 10)+
                    DateTimeUtilT.dateTime(new Date()), "没有采集到数据");
            return new HttpResult(HttpResult.NULL, "没有采集到数据");
        }
        Boolean b = null;
        int success = 0;
        int unseccess = 0;
        StringBuffer videoStr = new StringBuffer("");
        StringBuffer videoStr1 = new StringBuffer("");
        videoStr.append(url + "\n");
        for (Video video : list) {

            b = saveVideo(video);
            if (b) {
                success++;
                videoStr1.append("电视剧title====" + video.getTitle() + "====id=" + video.getId() + "\n");
            } else {
                unseccess++;
            }
        }
        videoStr.append("采集到电视剧总数:" + list.size() + "个，更新成功" + success + "个。失败:" + unseccess + "个\n");
        videoStr.append(videoStr1);
        redisTemplate.set("电视剧更新:" + RandCodeUtil.randNumberCodeByCustom("5", 10)+
                DateTimeUtilT.dateTime(new Date()), videoStr.toString());


        return new HttpResult(HttpResult.OK, videoStr);

    }

    @Override
    public HttpResult getAiqiyiVideo(Integer i) {
        List<Video> list = null;
        String url = "https://list.iqiyi.com/www/1/----------2---11-" + i + "-1-iqiyi--.html";
        list = URLFecter.URLParser(url, 1);
        if (list == null || list.size() <= 0) {
            redisTemplate.set("电影更新:" + RandCodeUtil.randNumberCodeByCustom("5", 10)+
                    DateTimeUtilT.dateTime(new Date()), "没有采集到数据");
            return new HttpResult(HttpResult.NULL, "没有采集到数据");
        }
        //循环输出抓取的数据
        Boolean b = null;
        int success = 0;
        int unseccess = 0;
        StringBuffer videoStr = new StringBuffer("");
        StringBuffer videoStr1 = new StringBuffer("");
        videoStr.append(url + "\n");
        for (Video video : list) {
            b = saveVideo(video);
            if (b) {
                success++;
                videoStr1.append("电影title====" + video.getTitle() + "====id=" + video.getId() + "\n");
            } else {
                unseccess++;
            }
        }
        videoStr.append("采集到视频总数:" + list.size() + "个，更新成功" + success + "个。失败:" + unseccess + "个\n");
        videoStr.append(videoStr1);
        redisTemplate.set("电影更新:" + RandCodeUtil.randNumberCodeByCustom("5", 10)+
                DateTimeUtilT.dateTime(new Date()), videoStr.toString());
        return new HttpResult(HttpResult.OK, videoStr);
    }


    @Override
    public Boolean getVideoByVid(String vid) {

        Map<String, Object> map = new HashMap<>();
        map.put("vid", vid);
        List<Video> list = getEntityByMap(Video.class, map);
        if (list != null && list.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public void getTVPlayDefailt(int type) {

        Map<String,Object> map = new HashMap<>();
        map.put("type",type);
        List<Video> list = getEntityByMap(Video.class,map);
        List<TVPlay> totalList = new ArrayList<>();
        Thread thread = new Thread(new VideoServiceImpl());
        if(list == null || list.size()<= 0) return;
        for(Video video : list){
            //爬取
            List<TVPlay> tvlist = URLFecter.getAiqiyiTVPlayList(video);
            List<TVPlay> tvlist1 = tvPlayService.getTVPlayByVid(video.getId());
            if((tvlist.size() == 0 || tvlist == null) && (tvlist1 == null || tvlist1.size() == 0)){
                deleteEntityById(video.getId(),Video.class);
            }if(tvlist.size() != tvlist.size()){
                tvPlayService.deleteTVPlayByVid(video.getId());
                tvPlayService.addTVPlayByList(tvlist);
            }
        }
    }

    @Override
    public Boolean saveVideo(Video video) {
        System.out.println("保存视频" + video);
        Boolean b = getVideoByVid(video.getVid());
        if (!b) {
            Long id = addEntityReturnId(video);
            video.setId(id);
            if(video.getType() == 2 || video.getType() == 4){
                List<TVPlay> list = URLFecter.getAiqiyiTVPlayList(video);
                tvPlayService.addTVPlayByList(list);
            }
            return true;
        }
        return false;
    }


    @Override
    public Video getVideoById(Long id) {

        return getEntityById(id, Video.class);
    }

    @Override
    public HttpResult getVideoList(int page) {

        List<Video> list = new ArrayList<>();
        list = getEntitiesBySort(Video.class, null, "id", true);
        return new HttpResult(HttpResult.OK, new PageUtil(page, 18, list));
    }

    @Override
    public HttpResult getVideoByPage(int cur, int max) {
        Map<String, Object> map = new HashMap<>();
        List<Video> list = findEntityByPage(Video.class, map, cur, max, true);
        return new HttpResult(HttpResult.OK, list);
    }

    @Override
    public List<Video> getVideoListByPage(int cur, int max,int type) {

        Map<String, Object> map = new HashMap<>();
        if(type != 0){
            map.put("type",type);
        }
        List<Video> list = findEntityByPage(Video.class, map, cur, max, true);
        return list;
    }

    @Override
    public int getVideoCountByType(int type) {
        Map<String, Object> map = new HashMap<>();
        if (type != 0)
            map.put("type", type);
        int count = getCountByMap(Video.class, map);
        return count;
    }


    @Override
    public List<Video> getVideoByTitle(String title) {

        List<Video> list = videoDao.getVideoByTitle(title);
        return list;
    }

    @Override
    public void uploadImageToOss() {
        Thread thread = new Thread(new VideoServiceImpl());
        List<Video> list = loadAllEntity(Video.class);
        for (Video video : list) {
            String imageurl = video.getImage();
            if (imageurl.startsWith("//")) {
                imageurl = "http:" + imageurl;
                String url = UploadImageOSS.uploadOSS(imageurl);
                try {
                    Thread.sleep(3000L);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                System.out.println("上传成功" + url);
                video.setImage(url);
                updateVideo(video);
            }
        }
    }

    @Override
    public void run() {
    }

    @Override
    public Boolean updateVideo(Video video) {

        return updateEntityValues(video);
    }

    @Override
    public List<Video> getAllVideo() {
        return loadAllEntity(Video.class);
    }
}
